package com.damieoa.damieoa.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.damieoa.damieoa.dao.StaffMapper;
import com.damieoa.damieoa.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    //添加员工并添加该员工的状态信息
    @Override
    public void addStaff(Staff staff) {
        staffMapper.insert(staff);
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card_number", staff.getId_card_number());
        //获取新增员工的id,创建该员工id与其状态表id对应的表
        Staff staff_id = staffMapper.selectOne(queryWrapper);
        staffMapper.addStaffStatus(staff_id.getId());
    }

    //员工信息查询
    @Override
    public List<Staff> queryStaff(Staff staff) {
        if (staff.getQuery_way().equals("user_name") ||
                staff.getQuery_way().equals("id") ||
                staff.getQuery_way().equals("department") ||
                staff.getQuery_way().equals("station")) {
            QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(staff.getQuery_way(), staff.getQuery_conditions());
            return staffMapper.selectList(queryWrapper);
        } else if (staff.getQuery_way().equals("all")) {
            QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("user_name", staff.getQuery_conditions())
                    .or().like("id", staff.getQuery_conditions())
                    .or().like("department", staff.getQuery_conditions())
                    .or().like("station", staff.getQuery_conditions());
            return staffMapper.selectList(queryWrapper);
        }
        return null;
    }

    //员工信息删除
    @Override
    public void deleteStaff(Staff staff) {
        staffMapper.deleteStaff(staff);
    }

    //员工详细信息查询
    @Override
    public Staff particularQueryStaff(Staff staff) {
        return staffMapper.selectById(staff);
    }

    //员工详细信息修改
    @Override
    public void updateStaff(Staff staff) {
        staffMapper.updateById(staff);
    }
}
